export default {
    data() {
        return {
            searchVal: '',
            isShowSuggestion: false
        }
    },
    computed: {
        // 计算是否显示历史记录模块
        showHistory() {
            return !(this.$store.state.tagsHistory.length === 0)
        }
    },
    methods: {
        // 搜索：添加历史记录并跳转
        search(event) {
            this.isShowSuggestion = false
            if (event.key === 'Enter') {
                if (this.$store.state.tagsHistory.indexOf(this.searchVal.trim()) === -1) {
                    this.$store.state.tagsHistory.unshift(this.searchVal.trim())
                }
            } else {
                if (this.$store.state.tagsHistory.indexOf(event.target.innerText) === -1) {
                    this.$store.state.tagsHistory.unshift(event.target.innerText)
                }
                this.isShowSuggestion = false
            }
            this.$router.push({
                path: '/searchResult'
            })
        },
        showSuggestion(event) {
            if (event.key !== '') {
                this.isShowSuggestion = true
            } else {
                this.isShowSuggestion = false
            }
        },
        // 清除历史记录
        clearHistory() {
            this.$store.state.tagsHistory = []
        },
        onSearch(val) {
            console.log(val)
            this.isShowSuggestion = true;
        },
        onCancel() {
            window.history.length > 1 ? this.$router.go(-1) : this.$router.push('/')
        }
    }
}